home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / dakit / dalloc.asm < prev    next >
Encoding:
Assembly Source File  |  1990-10-10  |  2.4 KB  |  102 lines

  1. ;-----------------------------------------------------------------
  2. ; dalloc.asm
  3. ; ----------
  4. ; Memory-allocation routines that operate OUTSIDE of the data segment.
  5. ; These work with SEGMENT addresses, NOT actual pointers.
  6. ; For allocation in the data segment, just use malloc () and free ().
  7. ;-----------------------------------------------------------------
  8.  
  9.     include asm.inc
  10.  
  11.     HEADER dalloc
  12.  
  13.     PSEG dalloc
  14.  
  15. ;-----------------------------------------------------------------
  16. ; WORD DAlloc (size);
  17. ;    UWORD size;
  18. ; Allocate a block of "size" bytes and return its segment.  If
  19. ; the allocation fails, return NULL.
  20. ; Since this uses DOS allocation, it will allocate just enough paragraphs
  21. ; for the number of bytes requested.
  22. ;-----------------------------------------------------------------
  23.  
  24.     public _DAlloc
  25.     STARTPROC _DAlloc
  26.  
  27.     push    bp
  28.     mov    bp,sp
  29.  
  30.     mov    ah,048h        ; function #
  31.  
  32. ; bx = number of paragraphs = (bytes >> 4) + (bytes & 0x0f) ? 1 : 0;
  33.     mov    bx,ARGB[bp]
  34.     mov    cl,4
  35.     shr    bx,cl
  36.     test    word ptr ARGB[bp],0fh
  37.     je    dalloc_p0
  38.     inc    bx
  39. dalloc_p0:
  40.  
  41.     int    021h
  42.     jnc    dalloc_p1
  43.     xor    ax,ax        ; alloc failed, so return NULL
  44. dalloc_p1:
  45.  
  46.     pop    bp
  47.     ret
  48.  
  49.     ENDPROC _DAlloc
  50.  
  51. ;-----------------------------------------------------------------
  52. ; void SFree (seg);
  53. ; Was "DFree", but PLink said that conflicted with "dfree".
  54. ; Free the block at the given segment address.
  55. ;
  56. ; Okay to call with NULL, so safe to call on already freed segment
  57. ; variables.  That DOESN'T mean it is okay to maintain two DIFFERENT
  58. ; segment variables, and try to free the same region twice through
  59. ; two different variables!  Also, it is up to the caller to NULL
  60. ; his sgment variable when done.  Maybe we should switch to a model
  61. ; where caller passes ADDRESS of segment variable! [sss]
  62. ; Always returns null, so can use to null a variable holding a
  63. ; segment address:
  64. ;    "segname = SFree(segname);"
  65. ;-----------------------------------------------------------------
  66.  
  67.     public _SFree
  68.     STARTPROC _SFree
  69.  
  70.     push    bp
  71.     mov    bp,sp
  72.     push    es
  73.  
  74. ; if (!seg), don't do anything
  75.     test    word ptr ARGB[bp],0ffffh
  76.     jz    dfree_done
  77.  
  78. ; free the segment
  79.     mov    ah,049h
  80.     mov    es,ARGB[bp]
  81.     int    021h
  82. ifdef DEBUGGING
  83.     jnc    dfree_1
  84.     mov    ax,9999
  85.     push    ax
  86.     call    _crash
  87. dfree_1:
  88. endif
  89.  
  90. dfree_done:
  91.     xor    ax,ax        ; NULL return value so can assign.
  92.     pop    es
  93.     pop    bp
  94.     ret
  95.  
  96.     ENDPROC _SFree
  97.  
  98. ;-----------------------------------------------------------------
  99.  
  100.     ENDPS dalloc
  101.     end
  102.